package com.samen.imgtools;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class ImageTools {
	/**
	 * Resizes the bitmap to fit in the window of given width and height
	 * 
	 * @param bitmap
	 *            - bitmap to resize
	 * @param width
	 *            - width in pixels
	 * @param height
	 *            - height in pixels
	 * @return - resized bitmap
	 */
	public static Bitmap resizeiBitmapToFit(Bitmap bitmap, int width, int height) {
		int originalWidth = bitmap.getWidth(), originalHeight = bitmap.getHeight();
		float ratioW = width / (float) originalWidth;
		float rationH = height / (float) originalHeight;
		float ratio = Math.min(ratioW, rationH);
		return Bitmap.createScaledBitmap(bitmap, (int) (originalWidth * ratio), (int) (originalHeight * ratio), false);
	}

	/**
	 * Resizes bitmap to exact size given by width and height. If the bitmap does not fit the given exactly, it creates a transparent bitmap of given size which includes the given
	 * resized bitmap
	 * 
	 * @param bitmap
	 *            - bitmap to reisze
	 * @param width
	 *            - target width in pixels
	 * @param hieght
	 *            - target height in pixels
	 * @param useTransparentRegion
	 *            - true if
	 * @return resized bitmap
	 */
	public static Bitmap resizeBitmapToSize(Bitmap bitmap, int width, int height) {
		bitmap = resizeiBitmapToFit(bitmap, width, height);
		Bitmap wideBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
		Canvas canvas = new Canvas(wideBitmap);
		canvas.drawBitmap(bitmap, (width - bitmap.getWidth()) / 2, (height - bitmap.getHeight()) / 2, null);
		return wideBitmap;
	}

}
